Skip to content

Method: static {...}

1: /*
2: * *********************************************************************************************************************
3: *
4: * Mistral: open source imaging engine
5: * http://tidalwave.it/projects/mistral
6: *
7: * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/mistral-src
23: * git clone https://github.com/tidalwave-it/mistral-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.image.processor;
28:
29: import java.util.ArrayList;
30: import java.util.List;
31: import java.io.Serializable;
32: import it.tidalwave.image.processor.event.ImagingTaskProcessorEvent;
33: import it.tidalwave.image.processor.event.ImagingTaskProcessorListener;
34: import lombok.extern.slf4j.Slf4j;
35:
36: /***********************************************************************************************************************
37: *
38: * @author Fabrizio Giudici
39: *
40: **********************************************************************************************************************/
41: @Slf4j
42: /* package */ class ImagingTaskProcessorEventManager
43: {
44: private ImagingTaskProcessor processor;
45:
46: private List<ImagingTaskProcessorListener> listeners = new ArrayList<>();
47:
48: /*******************************************************************************************************************
49: *
50: *
51: ******************************************************************************************************************/
52: public ImagingTaskProcessorEventManager (final ImagingTaskProcessor processor)
53: {
54: this.processor = processor;
55: }
56:
57: /*******************************************************************************************************************
58: *
59: *
60: ******************************************************************************************************************/
61: public void addListener (final ImagingTaskProcessorListener listener)
62: {
63: listeners.add(listener);
64: }
65:
66: /*******************************************************************************************************************
67: *
68: *
69: ******************************************************************************************************************/
70: public void removeListener (final ImagingTaskProcessorListener listener)
71: {
72: listeners.remove(listener);
73: }
74:
75: /*******************************************************************************************************************
76: *
77: * Guarantees that the controller is invoked in thread-safe mode and that
78: * the counters are not changed while the controller is running.
79: *
80: ******************************************************************************************************************/
81: public void fireNotifyTaskPosted (final ImagingTask task)
82: {
83: synchronized (processor.lock)
84: {
85: final var event =
86: new ImagingTaskProcessorEvent(processor, Thread.currentThread().getName(), task);
87:
88: for (final var listener : new ArrayList<>(listeners))
89: {
90: try
91: {
92: listener.notifyTaskPosted(event);
93: }
94: catch (Throwable t)
95: {
96: log.warn("Exception in fireNotifyTaskPosted(): " + t);
97: log.warn("fireNotifyTaskPosted()", t);
98: }
99: }
100: }
101: }
102:
103: /*******************************************************************************************************************
104: *
105: * Guarantees that the controller is invoked in thread-safe mode and that
106: * the counters are not changed while the controller is running.
107: *
108: ******************************************************************************************************************/
109: public void fireNotifyTaskStarted (final ImagingTask task, final Serializable workerId)
110: {
111: synchronized (processor.lock)
112: {
113: final var event = new ImagingTaskProcessorEvent(processor, workerId, task);
114:
115: for (final var listener : new ArrayList<>(listeners))
116: {
117: try
118: {
119: listener.notifyTaskStarted(event);
120: }
121: catch (Throwable t)
122: {
123: log.warn("Exception in fireNotifyTaskStarted(): " + t);
124: log.warn("fireNotifyTaskStarted()", t);
125: }
126: }
127: }
128: }
129:
130: /*******************************************************************************************************************
131: *
132: * Guarantees that the controller is invoked in thread-safe mode and that
133: * the counters are not changed while the controller is running.
134: *
135: ******************************************************************************************************************/
136: public void fireNotifyTaskCompleted (final ImagingTask task)
137: {
138: synchronized (processor.lock)
139: {
140: final var event =
141: new ImagingTaskProcessorEvent(processor, Thread.currentThread().getName(), task);
142:
143: for (final var listener : new ArrayList<>(listeners))
144: {
145: try
146: {
147: listener.notifyTaskCompleted(event);
148: }
149: catch (Throwable t)
150: {
151: log.warn("Exception in fireNotifyTaskCompleted(): " + t);
152: log.warn("fireNotifyTaskCompleted()", t);
153: }
154: }
155: }
156: }
157:
158: /*******************************************************************************************************************
159: *
160: * Guarantees that the controller is invoked in thread-safe mode and that
161: * the counters are not changed while the controller is running.
162: *
163: ******************************************************************************************************************/
164: public void fireNotifyTaskPopped (final ImagingTask task)
165: {
166: synchronized (processor.lock)
167: {
168: final var event =
169: new ImagingTaskProcessorEvent(processor, Thread.currentThread().getName(), task);
170:
171: for (final var listener : new ArrayList<>(listeners))
172: {
173: try
174: {
175: listener.notifyTaskPopped(event);
176: }
177: catch (Throwable t)
178: {
179: log.warn("Exception in fireNotifyTaskPopped(): " + t);
180: log.warn("fireNotifyTaskPopped()", t);
181: }
182: }
183: }
184: }
185: }